#' R version 4.3.0 (2023-04-21)
#' platform       aarch64-apple-darwin20
#' Produce map for official state language

library(ggplot2)
library(ggpattern)
library(sf)
library(dplyr)
library(magick)

# PRELIM -----------------------------------------------------------------------
MAIN <- "" # change this line
if (Sys.info()["user"] == "guan"){
  MAIN <- "/Users/guan/Dropbox/India_Language/replication_RESTAT"
} 
if (MAIN == ""){
  stop("please specify root directory")
}

GIS <- file.path(MAIN, "gis")
RAW <- file.path(MAIN, "raw")
PROC <- file.path(MAIN, "proc")

# DATA & MAP -------------------------------------------------------------------
# Shapefile
IND_adm1 <- file.path(GIS, "IND_adm", "IND_adm1.shp") %>% 
  st_read() %>%
  mutate(
    ID_KCClang = case_match(
      ID_KCClang, 
      4 ~ 3, 
      7 ~ 6,
      9 ~ 8,
      .default = ID_KCClang
  ),
  KCC_lang = if_else(KCC_lang == "Dogri, Kashmiri, Ladakhi", "Dogri", KCC_lang)
  )

# Create a vector of 19 different valid patterns with variations
patterns <- c(
  "stripe", "stripe", "stripe", "stripe", "stripe", 
  "none", "crosshatch", "crosshatch", "crosshatch", "crosshatch", 
  "circle", "circle", "circle", "circle", "circle", 
  "none", "none", "none", "weave"
)

# Add more variation in angles and spacings
angles <- c(
  0, 30, 45, 135, 90, 
  0, 0, 45, 45, 60, 
  0, 0, 0, 0, 0, 
  15, 30, 0, 45
)

spacings <- c(
  0.01, 0.02, 0.03, 0.03, 0.01, 
  0.01, 0.01, 0.05, 0.03, 0.02, 
  0.01, 0.02, 0.03, 0.04, 0.04, 
  0.01, 0.03, 0.01, 0.03
)

pattern_fills <- c("#FFFFFF", "#F0F0F0", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "white",
                  "#FFFFFF", "#F0F0F0", "#D9D9D9", "#BDBDBD", "white", "#737373", "#525252",
                  "#FFFFFF", "#F0F0F0", "#D9D9D9", "#BDBDBD", "#525252")

color_fills <- c(
  "grey90", NA, "grey70", NA, NA,
  "grey70", NA, "grey50", "grey90", "grey50",
  "grey50", NA, "grey50", NA, "grey50", 
  "grey30", "grey90", "white", "grey70"
)

# Create the map
p <- ggplot(data = IND_adm1) +
  # First layer: patterns
  geom_sf_pattern(
    aes(
      #fill = "white", 
      pattern = KCC_lang,
      pattern_angle = KCC_lang,
      pattern_spacing = KCC_lang,
      fill = KCC_lang
    ),
    pattern_colour = "black",
    pattern_fill = "white"
    ) +
  # Second layer: borders
  geom_sf(
    fill = NA,
    colour = "black",
    size = 1  # Adjust the line width as needed
  ) +
  scale_fill_manual(values = color_fills, na.value = "white") +
  scale_pattern_manual(values = patterns) +
  scale_pattern_angle_manual(values = angles) +
  scale_pattern_spacing_manual(values = spacings) +
  scale_pattern_fill_manual(values = pattern_fills) +
  theme_minimal() +
  theme(
    panel.grid = element_blank(),
    axis.title = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    legend.position = "right",
    legend.title = element_blank(),
    legend.text = element_text(size = 12)
  ) +
  guides(
    fill = guide_legend(
      override.aes = list(
        pattern = patterns, 
        pattern_angle = angles, 
        pattern_spacing = spacings,
        pattern_fill = pattern_fills
      )
    )
  )

# OUTPUT -----------------------------------------------------------------------
# this is a workaround to save a pdf version
ggsave(filename = "india_kcc_all_cells.png", plot = p, device = "png", path = file.path(MAIN, "results","graphs"), width = 6, height = 4.9, dpi = 300)
image <- file.path(MAIN, "results","graphs","india_kcc_all_cells.png") %>% image_read()
image_write(image, path = file.path(MAIN, "results","graphs","figure_1a_india_kcc_all_cells.pdf") , format = "pdf")
